package com.cn.wanxi.util;

import com.cn.wanxi.dao.CompanyDao;

import java.io.*;
import java.util.List;

public class IOTool {
    private static File file = null;

    private IOTool() {
    }

    private static void createNote(String fileName) {
        File derectory = new File(Path.PATH);
        if (!derectory.exists()) {
            derectory.mkdirs();
        }
        File tempFile = new File(Path.PATH, fileName);
        try {
            tempFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        就近原则
        file = tempFile;
    }

    public static String readNote() {
        StringBuilder result = new StringBuilder();
//        String和StringBuilder，和StringBuffer的区别
        try {
//            InputStream inputStream = new FileInputStream(file);
//            int read = 0;
////            byte[] bytes = new byte[1024];
//            while (read != -1) {
//                read = inputStream.read();
//                System.out.print((char) read);
//            }
            Reader reader = new FileReader(file);
            int read = 0;

            while (read != -1) {
                read = reader.read();
                result.append((char) read);
//                result = result + (char) read;
            }
            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString().substring(0, result.length() - 1);
    }

    public static void wirteNote(String fileName, List<String> list) {
        createNote(fileName);
//耦合度
        try {
            OutputStream outputStream = new FileOutputStream(file);
//            CompanyDao companyDao = new CompanyDao();
            for (String value : list
            ) {
                outputStream.write(value.getBytes());
                outputStream.write(Path.SPLIT.getBytes());
            }

//            outputStream.write(companyDao.address.getBytes());
//            outputStream.write(Path.SPLIT.getBytes());
//            outputStream.write(companyDao.getPhone().getBytes());
//            outputStream.write(Path.SPLIT.getBytes());
//            outputStream.write(companyDao.net.getBytes());
//            outputStream.write(Path.SPLIT.getBytes());
//            outputStream.write(companyDao.version.getBytes());
//            outputStream.write(Path.SPLIT.getBytes());
//            outputStream.write(companyDao.email.getBytes());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
